package com.zhia.demo.controller;

import com.zhia.demo.service.UserLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

/**
 * @author DONG
 * @create 2023/11/5 14:25
 */
@RestController
public class UserLikeController {
    @Autowired
    UserLikeService userLikeService;

    @PostMapping("/like/{reviewId}")
    public void like(@PathVariable("reviewId") String reviewId,@CookieValue(name = "USERSESSION", defaultValue = "") String cookie) {
        if(!StringUtils.isEmpty(cookie)) {
            userLikeService.addLike(reviewId,Integer.parseInt(cookie));
        }
    }

    @DeleteMapping("/unlike/{reviewId}")
    public void unlike(@PathVariable("reviewId") String reviewId,@CookieValue(name = "USERSESSION", defaultValue = "") String cookie) {
        if(!StringUtils.isEmpty(cookie)) {
            userLikeService.cancelLike(reviewId,Integer.parseInt(cookie));
        }
    }
}
